<?php

namespace SinoPHP\BbdBoss\Model;

use Upfor\UpforPHP\Model;

/**
 * 客户域名解析关系
 */
class CustomerDomainRelation extends Model {

    /**
     * 根据ID查询客户域名信息
     *
     * @param  integer $domainId
     * @return array
     */
    public function getDomainInfoById($domainId) {
        return $this->db->table('customer_domain')->where([
            'domain_id' => $domainId,
            'is_delete' => 0,
        ])->getRow();
    }

    /**
     * 根据层级查询域名的记录列表
     *
     * @param  integer $domainId
     * @param  integer $layer
     * @return array
     */
    public function getRelationRecordListByDomainAndLayer($domainId, $layer) {
        return $this->db->table('customer_domain_relation')->field([
            'layer_relation_id',
            'domain_id',
            'record_layer',
            'record_line_name',
            'record_line_id',
            'ttl',
            'inner_domain_id',
            'record',
            'etcd_status',
            'create_time',
        ])->where([
            'domain_id' => $domainId,
            'record_layer' => $layer,
            'is_delete' => 0,
        ])->order('record_line_id', 'ASC')->getList();
    }

    /**
     * 禁用某条记录
     *
     * @param  integer $relationId
     * @return int
     */
    public function unableRelationRecord($relationId) {
        return $this->db->table('customer_domain_relation')->where([
            'layer_relation_id' => $relationId,
        ])->update(array(
            'etcd_status' => 0,
            'is_delete' => 1,
            'update_time' => time(),
        ));
    }

    /**
     * 根据ID查询内部域名信息
     *
     * @param  integer $innerDomainId
     * @return array
     */
    public function getInnerDomainInfoById($innerDomainId) {
        return $this->db->table('inner_domain')->where([
            'inner_domain_id' => $innerDomainId,
        ])->getRow();
    }

    /**
     * 查询全部内部域名列表
     *
     * @return array
     */
    public function getInnerDomainList() {
        $data = $this->db->table('inner_domain')->field([
            'inner_domain_id',
            'domain_name',
            'domain_suffix',
        ])->order('create_time', 'DESC')->getList();

        $innerDomainIds = array_column($data, 'inner_domain_id');
        $masterRecordData = $this->db->table('inner_domain_master_record')->field([
            'master_record_id',
            'inner_domain_id',
        ])->where('inner_domain_id', $innerDomainIds)->group('inner_domain_id')->getList();

        $masterRecordList = array();
        foreach ($masterRecordData as $row) {
            $masterRecordList[$row['inner_domain_id']] = !empty($row['master_record_id']);
        }

        $innerDomainList = [];
        foreach ($data as $row) {
            if (!empty($masterRecordList[$row['inner_domain_id']])) {
                $innerDomainList[] = $row;
            }
        }

        return $innerDomainList;
    }

    /**
     * 批量插入记录
     *
     * @param  array $recordList
     * @return int
     */
    public function insertRelationRecords(array $recordList) {
        return $this->db->table('customer_domain_relation')->insert($recordList);
    }

    /**
     * 记录ETCD操作日志
     *
     * @param  array $data
     * @return integer
     */
    public function insertEtcdLog(array $data) {
        return $this->db->table('domain_dnspod_etcd_log')->insert($data);
    }

    /**
     * 根据ID更新记录值
     *
     * @param  integer $relationId
     * @param  array   $data
     * @return integer
     */
    public function updateRelationRecordById($relationId, array $data) {
        return $this->db->table('customer_domain_relation')->where('layer_relation_id', $relationId)->update($data);
    }

    /**
     * 根据ID查询记录详情
     *
     * @param  integer $relationId
     * @return array
     */
    public function getRelationRecordDetailById($relationId) {
        return $this->db->table('customer_domain_relation')->field([
            'layer_relation_id',
            'domain_id',
            'record_layer',
            'record_line_name',
            'record_line_id',
            'ttl',
            'inner_domain_id',
            'record',
            'etcd_status',
            'create_time',
            'is_delete',
        ])->where([
            'layer_relation_id' => $relationId,
            'is_delete' => 0,
        ])->getRow();
    }

    /**
     * 更新客户域名最后修改时间
     *
     * @param  integer $domainId
     */
    public function domainUpdateTime($domainId) {
        $this->db->table('customer_domain')->where([
            'domain_id' => $domainId,
        ])->update([
            'update_time' => time(),
        ]);
    }

}
